<?php

namespace app\admin\controller;


use app\admin\model\order\OrderModel;
use app\api\model\settings\DicModel;
use app\api\model\store\Clerk;
use app\common\enum\order\OrderPayStatusEnum;
use app\common\library\sms\Driver as SmsDriver;
use app\common\model\plus\seckill\SeckillSku as ProductSkuModel;
use app\common\model\settings\Setting as SettingModel;
use app\Request;
use app\common\model\plus\seckill\Product as ProductModel;

/**
 * 全局订单类
 */
class Order extends Controller
{

    public function paySuccess(Request $request)
    {
        $params = $request->param();
//        log_write('回调参数' . json_encode($params));
        $order_no = $params['out_trade_no'];
        $orderModel = new OrderModel();
        $orderModel->paySuccess($order_no);
        echo 1;
    }

//    public function cancelSekillOrder()
//    {
//        $model = new \app\common\model\order\Order();
//        $model->transaction(function () use ($model) {
//            $conn = new \think\cache\driver\Redis(config('cache.stores.redis'));
//            $list = $model->where(['pay_status' => 10, 'order_source' => 50, 'order_status' => 10])->with(['deliveryUser', 'product' => ['image', 'refund'], 'address', 'express', 'extractStore', 'supplier', 'advance'])->limit(0, 100)->select();
//            foreach ($list as &$item) {
//                foreach ($item['product'] as $items) {
//                    $sku = ProductSkuModel::detail($items['sku_source_id']);
//                    $conn->watch($sku['seckill_product_sku_id'] . '_seckill_stock');
//                    $conn->watch($sku['seckill_product_id'] . '_product_stock');
//                    $conn->multi();
//                    $product = (new ProductModel)->where('seckill_product_id', '=', $sku['seckill_product_id'])->find();
//                    $productsku = (new ProductSkuModel)->where('seckill_product_sku_id', '=', $sku['seckill_product_sku_id'])->find();
//                    $conn->set($sku['seckill_product_sku_id'] . '_seckill_stock', $productsku['seckill_stock'] + $items['total_num']);
//                    $conn->set($sku['seckill_product_id'] . '_product_stock', $product['stock'] + $items['total_num']);
//                    $tag = $conn->exec();
//                    if ($tag) {
//                        // 回退主库存
//                        (new ProductModel)->where('seckill_product_id', '=', $sku['seckill_product_id'])->inc('stock', $items['total_num'])->update();
//                        // 回退sku库存
//                        (new ProductSkuModel)->where('seckill_product_sku_id', '=', $sku['seckill_product_sku_id'])->inc('seckill_stock', $items['total_num'])->update();
//                    }
//                }
//                $item->save(['order_status' => 20]);
//            }
//            echo count($list) . "条记录处理完毕";
//        });
//    }

    public function overTimeOrderMsg()
    {
        $model = new \app\common\model\order\Order();
        $now = time();
        $list = $model->where([
            'pay_status' => OrderPayStatusEnum::SUCCESS,
            'delivery_status' => 20,
            'receipt_status' => 10,
            'order_status' => 10
        ])->where(['is_over_time_msg' => 0])->limit(0, 100)->select();
        $time = DicModel::listAll(['type' => 'sendTime'], [], 'sort_num desc');
        foreach ($list as $item) {
            $newTimestamp = strtotime('+' . $time[0]['value'] . ' minutes', $item['delivery_time']); // 将当前时间戳加上25分钟
            $newTimestamp = strtotime('-5 minutes', $newTimestamp);
            if ($now >= $newTimestamp) {
                //发短信
                $clerk = Clerk::where('clerk_id', '=', $item['store_clerk_id']) -> where('is_delete',0) -> where('is_send',1) ->find();
                if (!empty($clerk['mobile'])) {
                    $smsConfig = SettingModel::getItem('sms', '10001');
                    $send_template = 'SMS_464470707';
                    $SmsDriver = new SmsDriver($smsConfig);
                    $mobile = $clerk['mobile'];
                    $send_data = [
                        'time' => date('Y-m-d H:i:s', $item['delivery_time']),
                        'order_id' => $item['order_no']
                    ];
//                    $flag = $SmsDriver->sendSms($mobile, $send_template, $send_data);
//                    if ($flag) {
//                        $item['is_over_time_msg'] = 1;
//                        $item->save();
//                    }
                }
            }
        }
    }

}
